package com.gadgets.framework.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-09-22
 * 
 */
public class FileOperUtil {

    private static final Logger logger = LoggerFactory.getLogger(FileOperUtil.class);

    /**
     * 将文本写入指定路径
     * @param filePath  文件路径
     * @param txt       文本内容
     */
    public static void writeTxtTotFile(String filePath, String txt){
        File file = new File(filePath);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                logger.error(StringUtils.formatExceptionStack(e));
                return;
            }
        }
        OutputStreamWriter streamWriter = null;
        PrintWriter printWriter = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file,true);
            streamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
            printWriter = new PrintWriter(streamWriter);
            printWriter.write(txt);
            printWriter.flush();
        } catch (IOException e) {
            logger.error(StringUtils.formatExceptionStack(e));
        } finally {
            if(null!=printWriter){
                try {
                    printWriter.close();
                } catch (Exception e){
                    logger.error(StringUtils.formatExceptionStack(e));
                }
            }
            if(null!=streamWriter){
                try {
                    streamWriter.close();
                } catch (IOException e) {
                    logger.error(StringUtils.formatExceptionStack(e));
                }
            }
        }
    }

    /**
     * 将字节流写入指定路径
     * @param filePath  文件路径
     * @param byteArr   字节流
     */
    public static void writeStreamToFile(String filePath, byte[] byteArr){
        File file = new File(filePath);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                logger.error(StringUtils.formatExceptionStack(e));
                return;
            }
        }
        FileOutputStream outputStream = null;
        try{
            outputStream = new FileOutputStream(file);
            outputStream.write(byteArr, 0, byteArr.length);
            outputStream.flush();
        } catch (IOException e) {
            logger.error(StringUtils.formatExceptionStack(e));
        }finally {

            if(null!=outputStream){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    logger.error(StringUtils.formatExceptionStack(e));
                }
            }
        }
    }

    /**
     * 获取文件输出流
     * @param file                      文件对象
     * @return                          输出流对象
     * @throws FileNotFoundException    异常信息
     */
    protected static OutputStream getOutputStream(File file) throws FileNotFoundException {
        OutputStream out = null;
        out = new FileOutputStream(file);
        return out;
    }

    /**
     *
     * @param file
     * @return
     */
    public static int splitBigTxtFile(File file, String target) throws IOException {
        int i = 0;
        BufferedReader bufferedReader = FileReadUtil.fileReaderLine(file);
        String text = null;
        int line = 0;
        File dirTarget = new File(target);
        dirTarget.deleteOnExit();
        dirTarget.mkdir();
        System.out.println("current file "+i+".log");
        while (null!=(text= bufferedReader.readLine())){
            line++;
            writeTxtTotFile(target+"/"+i+".log", text);
            if(line>50000){
                line = 0;
                i++;
                System.out.println("current file "+i+".log");
            }
        }
        return i;
    }

}
